'use strict';

angular.module('Common').controller('ScheduleProgramController',
    ['$scope', '$stateParams', 'BaseModule', 'BaseFunction', '$location', 'Authentication',
    function($scope, $stateParams, BaseModule, BaseFunction, $location, Authentication) {
        //$scope.authentication = Authentication;
        $scope.EndModeEnum = BaseFunction.getEnum('CommonEnum.EndMode');
        $scope.btnSave = function(){
            if($scope.Module.ScheduleProgram.FocusedRecord.Code == undefined || 
                $scope.Module.ScheduleProgram.FocusedRecord.Code.trim() == ""){
                BaseFunction.alert("编码不能为空！", "保存错误");
                return;
            }
            if($scope.Module.ScheduleProgram.FocusedRecord.Name == undefined || 
                $scope.Module.ScheduleProgram.FocusedRecord.Name.trim() == ""){
                BaseFunction.alert("名称不能为空！", "保存错误");
                return;
            }
            if($scope.Module.ScheduleProgram.FocusedRecord.FirstTime == undefined || 
                $scope.Module.ScheduleProgram.FocusedRecord.FirstTime == null){
                BaseFunction.alert("首次时间不能为空！", "保存错误");
                return;
            }

            var intervals = 0;
            if($scope.Module.ScheduleProgram.FocusedRecord.Years != undefined){
                intervals += $scope.Module.ScheduleProgram.FocusedRecord.Years;
            }
            if($scope.Module.ScheduleProgram.FocusedRecord.Months != undefined){
                intervals += $scope.Module.ScheduleProgram.FocusedRecord.Months;
            }
            if($scope.Module.ScheduleProgram.FocusedRecord.Days != undefined){
                intervals += $scope.Module.ScheduleProgram.FocusedRecord.Days;
            }
            if($scope.Module.ScheduleProgram.FocusedRecord.Hours != undefined){
                intervals += $scope.Module.ScheduleProgram.FocusedRecord.Hours;
            }
            if($scope.Module.ScheduleProgram.FocusedRecord.Minutes != undefined){
                intervals += $scope.Module.ScheduleProgram.FocusedRecord.Minutes;
            }
            if($scope.Module.ScheduleProgram.FocusedRecord.Seconds != undefined){
                intervals += $scope.Module.ScheduleProgram.FocusedRecord.Seconds;
            }

            if(intervals == 0){
                BaseFunction.alert("执行间隔时间不能为0！年、月、日、时、分、秒至少有一个有值", "保存错误");
                return;
            }

            if($scope.Module.ScheduleProgram.FocusedRecord.EndMode == $scope.EndModeEnum.Repeat.Value){
                if($scope.Module.ScheduleProgram.FocusedRecord.RepeatTimes == undefined || 
                    $scope.Module.ScheduleProgram.FocusedRecord.RepeatTimes == null ||
                    $scope.Module.ScheduleProgram.FocusedRecord.RepeatTimes < 1){
                    BaseFunction.alert("当结束方式为 重复次数 时，重复次数必须大于零！", "保存错误");
                    return;
                }
            }
            if($scope.Module.ScheduleProgram.FocusedRecord.EndMode == $scope.EndModeEnum.EndTime.Value){
                if($scope.Module.ScheduleProgram.FocusedRecord.EndTime == undefined || 
                    $scope.Module.ScheduleProgram.FocusedRecord.EndTime == null){
                    BaseFunction.alert("当结束方式为 结束时间 时，结束时间不能为空！", "保存错误");
                    return;
                }
            }

            $scope.Module.ScheduleProgram.save();
        };
        $scope.btnCancel = function(){
            $scope.Module.ScheduleProgram.cancel();            
        };
        $scope.btnAdd = function(){
            $scope.Module.ScheduleProgram.add();
        };
        $scope.btnDelete = function(){
            $scope.Module.ScheduleProgram.delete();
        };
        $scope.btnList = function(){
            $location.url('/index/ScheduleProgram/List');
        };

        $scope.EndMode_Change = function(){
            $scope.Module.ScheduleProgram.FocusedRecord.EndTime = null;
            $scope.Module.ScheduleProgram.FocusedRecord.RepeatTimes = null;
        };

        //初始化
        BaseModule.initModule($scope, 'ScheduleProgram');
        $scope.Module.ScheduleProgram.AfterOnAdd = function(record){
            record.EndMode = $scope.EndModeEnum.Forever.Value;
        };
        
        if($stateParams.id) {
            $scope.Module.ScheduleProgram.findById($stateParams.id, function(record){
                $scope.Module.ScheduleProgram.FocusedRecord = record;
            });
        }
        else{
            $scope.btnAdd();
        };
    }
]);